# $Id$
# Maintainer: Connor Behan <connor.behan@gmail.com>
# Contributor: Jan de Groot <jgc@archlinux.org>
# Contributor: Alexander Baldeck <alexander@archlinux.org>

pkgname=xulrunner
pkgver=41.0.2
pkgrel=15
pkgdesc="Mozilla Runtime Environment"
arch=('loong64' 'x86_64')
license=('MPL' 'GPL' 'LGPL')
depends=('gtk2' 'mozilla-common' 'nss>3.18' 'libxt' 'hunspell' 'startup-notification' 'mime-types' 'dbus-glib' 'libpulse' 'libevent' 'libvpx' 'icu' 'python2')
makedepends=('gcc6' 'zip' 'unzip' 'pkg-config' 'diffutils' 'yasm' 'mesa' 'gconf' 'autoconf2.13' 'gst-plugins-base-libs')
url="http://wiki.mozilla.org/XUL:Xul_Runner"
source=(https://ftp.mozilla.org/pub/mozilla.org/xulrunner/releases/$pkgver/source/xulrunner-$pkgver.source.tar.xz
        mozconfig
        freetype261.patch
        0001-Bug-1233963-Work-around-recent-GNU-gold-behavior-wit.patch
	Bug-1269171.patch
        LFS_graphite2_harfbuzz.patch
        mozilla-pkgconfig.patch
        shared-libs.patch
        sed43.patch
        gcc6.diff)
options=('!emptydirs' '!makeflags' 'staticlibs')
replaces=('xulrunner-oss')
sha256sums=('f7abb2e2989779305ab1f80d30caf9fc55d96c7e66d1394e2cc9639442e2b864'
            '8ab57ae3ba4660a721cdb210babee02488b670d9af9fdecf5da3b832d77e342b'
            '3aea9a83bf304da5525f34a911712cf42f8ded1c8b6becf0a2cf8a4b4f7facd6'
            '13a27a385fab88938ac7417f1e7ca7225ec6b88840bafe67e0f5642e5446554a'
            'bd78da5d5319c8eb6a9195c3bd35bf919f9a9c030d69890f42e96171e32be60f'
            'afcd7d3f6113b0f9ead0cb1b47b3fccedadce56a16b4406c439411864bd11863'
            '1aa9ebe67542a2b8c28905d070829ada5b29438c6a7961f2b0cdd6b92d8b9f5c'
            '59d9fc421bc10a5515b73e159f44a72365bf7b7e8b3fc8a8c46043ef40bd3a40'
            'e9b7199b57fa07e440d37db0cc3cf52aa1c1fd7d5e8b31db14e511d77b5ce327'
            '0992d5dce883de760ff0445448466f096d0baa75fe5fd1e60bfd3cc13cb4d098')

prepare() {
  cd "$srcdir/mozilla-release"
  cp "$srcdir/mozconfig" .mozconfig

  # https://bugzilla.mozilla.org/show_bug.cgi?id=1329272
  patch -Np1 -i ../sed43.patch

  # https://bugzilla.mozilla.org/show_bug.cgi?id=1194520
  patch -Np1 -i ../freetype261.patch

  # https://bugzilla.mozilla.org/show_bug.cgi?id=1233963
  patch -Np1 -i ../0001-Bug-1233963-Work-around-recent-GNU-gold-behavior-wit.patch

  # It would be great to track down all the gcc7 fixes
  # https://bugzilla.mozilla.org/show_bug.cgi?id=1269171
  patch -Np1 -i ../gcc6.diff
  patch -Np1 -i ../Bug-1269171.patch

  # https://bugzilla.mozilla.org/show_bug.cgi?id=847568
  patch -Np1 -i ../LFS_graphite2_harfbuzz.patch
  sed -i -e "s|lcrmf|lm|g" configure.in
  sed -i -e "/lcrmf/d" config/external/nss/crmf/moz.build
  rm configure
  autoconf-2.13

  # fix libdir/sdkdir - fedora
  patch -Np1 -i ../mozilla-pkgconfig.patch
  patch -Np1 -i ../shared-libs.patch

  # WebRTC build tries to execute "python" and expects Python 2
  # Workaround taken from chromium PKGBUILD
  mkdir "$srcdir/python2-path"
  ln -s /usr/bin/python2 "$srcdir/python2-path/python"

  # configure script misdetects the preprocessor without an optimization level
  # https://bugs.archlinux.org/task/34644
  sed -i '/ac_cpp=/s/$CPPFLAGS/& -O2/' configure
}

build() {
  cd "$srcdir/mozilla-release"

  export PATH="$srcdir/python2-path:$PATH"
  export LDFLAGS="$LDFALGS -Wl,-rpath,/usr/lib/xulrunner-$pkgver"
  export PYTHON="/usr/bin/python2"

  make -j1 -f client.mk build
}

package() {
  cd "$srcdir/mozilla-release"
  make -j1 -f client.mk DESTDIR="$pkgdir" install

  # Use system-provided dictionaries
  rm -rf "$pkgdir"/usr/lib/xulrunner-$pkgver/{dictionaries,hyphenation}
  ln -sf /usr/share/hunspell "$pkgdir/usr/lib/xulrunner-$pkgver/dictionaries"
  ln -sf /usr/share/hyphen "$pkgdir/usr/lib/xulrunner-$pkgver/hyphenation"

  # add xulrunner library path to ld.so.conf
  install -d $pkgdir/etc/ld.so.conf.d
  echo "/usr/lib/xulrunner-$pkgver" > $pkgdir/etc/ld.so.conf.d/xulrunner.conf

  chmod +x "${pkgdir}/usr/lib/xulrunner-devel-$pkgver/sdk/bin/xpt.py"
  chmod +x "${pkgdir}/usr/lib/xulrunner-devel-$pkgver/sdk/bin/xpcshell"
  ln -s /usr/lib/xulrunner-devel-$pkgver/sdk/bin/xpcshell "${pkgdir}/usr/lib/xulrunner-$pkgver/xpcshell"
  sed -i 's|!/usr/bin/env python$|!/usr/bin/env python2|' \
    "$pkgdir"/usr/lib/xulrunner-devel-$pkgver/sdk/bin/{xpt,header,typelib,xpidl}.py
}
